package com.guodu.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.PurchaseProductionPlanDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.PurchaseProductionPlan;
import com.guodu.entity.PurchaseproductionStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.PurchaseProductionPlanService;
import com.guodu.vo.PurchaseProductionPlanQueryVo;
import com.guodu.vo.PurchaseProductionPlanVo;

@Service("purchaseProductionPlanService")
public class PurchaseProductionPlanServiceImpl implements PurchaseProductionPlanService {
	
	@Resource
	private PurchaseProductionPlanDao purchaseProductionPlanDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;


	@Override
	public void save(PurchaseProductionPlan purchaseProductionPlan) {
		// TODO Auto-generated method stub
		purchaseProductionPlanDao.save(purchaseProductionPlan);
	}

	@Override
	public long findSizeByVo(PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo, Units units) {
		// TODO Auto-generated method stub
		return purchaseProductionPlanDao.findSizeByVo(purchaseProductionPlanQueryVo,units);
	}

	@Override
	public List<PurchaseProductionPlanVo> findByVo(
			PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo, Units units, int curPage, int pageSize) {
		// TODO Auto-generated method stub
		List<PurchaseProductionPlan> list = purchaseProductionPlanDao.findByVo(purchaseProductionPlanQueryVo,units, curPage, pageSize);
		List<PurchaseProductionPlanVo> purchaseProductionPlanVos = new ArrayList<PurchaseProductionPlanVo>();
		for(PurchaseProductionPlan purchaseProductionPlan : list ){
			PurchaseProductionPlanVo purchaseProductionPlanVo = new PurchaseProductionPlanVo(purchaseProductionPlan);
			UserInfo userInfo = userInfoDao.findById(purchaseProductionPlan.getCreatePeople());
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			purchaseProductionPlanVo.setUnitsName(unitsInfo.getUnitsName());
			purchaseProductionPlanVo.setIndustry(unitsInfo.getIndustry());
			if(unitsInfo.getFunitsId()!=null){
				Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
				purchaseProductionPlanVo.setFunitsName(funitsInfo.getUnitsName());
			}
			purchaseProductionPlanVo.setUserName(userInfo.getUserName());
			purchaseProductionPlanVos.add(purchaseProductionPlanVo);
		}
		return purchaseProductionPlanVos;
	}

	@Override
	public PurchaseProductionPlan findById(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseProductionPlanDao.findById(purchaseId);
	}

	@Override
	public void update(PurchaseProductionPlan purchaseProductionPlan,Long status) {
		// TODO Auto-generated method stub
		purchaseProductionPlanDao.update(purchaseProductionPlan);
		List<PurchaseproductionStatus> entities = new ArrayList<PurchaseproductionStatus>();
		PurchaseproductionStatus entity = new PurchaseproductionStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(purchaseProductionPlan.getCreatePeople());
		entity.setPurchaseId(purchaseProductionPlan.getPurchaseId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		purchaseProductionPlanDao.changeStatus(entities);
	}

	@Override
	public List<PurchaseproductionStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return purchaseProductionPlanDao.findStatus(purchaseId);
	}

	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<PurchaseproductionStatus> entities = new ArrayList<PurchaseproductionStatus>();
		for(int i=0;i<idses.length;i++){
			PurchaseproductionStatus entity = new PurchaseproductionStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setPurchaseId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		purchaseProductionPlanDao.changeStatus(entities);
		purchaseProductionPlanDao.examine(ids,status);
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		purchaseProductionPlanDao.delByIds(delIds);
	}

	@Override
	public List<PurchaseProductionPlan> findAllByVo(
			PurchaseProductionPlanQueryVo pppq, Units attribute) {
		// TODO Auto-generated method stub
		return purchaseProductionPlanDao.findAllByVo(pppq,attribute);
	}

	@Override
	public void examine(
			PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		purchaseProductionPlanDao.examin(purchaseProductionPlanQueryVo, units);
	}

	@Override
	public PurchaseProductionPlanVo findAllMoney(
			PurchaseProductionPlanQueryVo purchaseProductionPlanQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		return purchaseProductionPlanDao.findAllMoney(purchaseProductionPlanQueryVo, units);
	}
}
